package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;

public class SzOnlineNewsAnalyse implements  AnalyseNews {

	//http://focus.szonline.net/Channel/201303/13/752927.shtm
	//http://v.szonline.net/c/3225362/
	//http://it.szonline.net/946395/620423246361b.shtml
	private static final String[] regex={
		"http://[a-z]+.szonline.net/contents/[0-9\\/]+.html",
		};
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}
	
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	

	/* (non-Javadoc)
	 * @see com.kdtech.analyse.news.AnalyseNews#parserHtml(com.kdtech.entity.crawler.UrlMeta)
	 */
	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		
		
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		if(doc.select("body").text().equals("您访问的链接不存在! 点击返回首页")){
			news.setUpdateUrl(ParseState.ERR404.toString());
			return news;
		}
		title=doc.select("title").text();
		title=StringUtils.substringBefore(title, "_");
		if(StringUtils.isBlank(title)){
			title=doc.select("div.s_title").text();
			title=StringUtils.substringAfter(title, ">>");
			if(StringUtils.isBlank(title)){
				title=doc.select("h3").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("h1").text();
				}
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div#cnt_content"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.nry_zw"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.main_text p"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.info p"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div.con"));
						if(StringUtils.isBlank(content)){
							content=HtmlCleaner.getContentHtml(url,doc.select("div.info h1"));
						}
					}
				}
			}
		}
		date=DateUtils.matchDate(doc.select("div.info").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div.service").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("p.s_txt").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("div.nry_wxcc").text());
					if(date==null){
						date=DateUtils.matchDate(doc.select("ul.layout_option").text());
						if(date==null){
							date=DateUtils.matchDate(url);
						}
					}
				}
			}
		}
		if(StringUtils.isBlank(title)){
		}
		news.setUrl(url);		
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		return news;
	}
	
	public static void main(String[] args) {
		SzOnlineNewsAnalyse test=new SzOnlineNewsAnalyse();
		String url="http://focus.szonline.net/contents/20151209/67971.html";
		System.out.println(DoMainUtils.GetDomainName(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=test.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
